package logic

import (
	"context"
	"inventory-system/model/mysql"

	"inventory-system/systemrpc/internal/svc"
	"inventory-system/systemrpc/systemrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *systemrpc.ListRequest) (*systemrpc.ListResponse, error) {
	// todo: add your logic here and delete this line

	var m []mysql.Product
	mysql.Db.Debug().Find(&m)

	var an []*systemrpc.List
	for _, i2 := range m {
		list := systemrpc.List{
			Title:     i2.Title,
			UserId:    int64(i2.UserId),
			Price:     float32(i2.Price),
			Stock:     int64(i2.Stock),
			Image:     i2.Image,
			ProductId: int64(i2.Stock),
		}
		an = append(an, &list)
	}

	return &systemrpc.ListResponse{
		List: an,
	}, nil
}
